import { createRouter, createWebHistory } from 'vue-router';
import { createPinia } from 'pinia';
import { useMainStore } from '@/stores/useMain';
import userRouter from './user';
import RegistrationManage from '@/views/registration/index.vue';

const pinia = createPinia();
const mainStore = useMainStore(pinia);

const routes = [
  ...userRouter,
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home.vue'),

    children: [
      {
        path: '',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
      },
      {
        path: 'account',
        name: 'account',
        component: () => import('@/views/account/index.vue'),
        meta: {
          title: '', // 账号管理
        },
      },
      {
        path: 'grading',
        name: 'grading',
        component: () => import('@/views/grading/index.vue'),
        meta: {
          title: '', // 考级申报
        },
        children: [
          {
            path: 'institution',
            name: 'gradingInstitution',
            component: () => import('@/views/grading/institution.vue'),
            meta: {
              title: '', // 考级机构
            },
          },
          {
            path: 'application',
            name: 'gradingApplication',
            component: () => import('@/views/grading/application.vue'),
            meta: {
              title: '', // 考试申报
            },
          },
        ],
      },
      {
        path: 'registration',
        name: 'RegistrationManage',
        component: RegistrationManage,
        meta: {
          title: '', // 考级报名管理
        },
      },
      {
        path: 'backExam',
        name: 'backExam',
        component: () => import('@/views/backExam/index.vue'),
        meta: {
          title: '', // 考级排考管理
        },
        children: [
          {
            path: 'arrange',
            name: 'examArrange',
            component: () => import('@/views/backExam/arrange.vue'),
            meta: {
              title: '', // 排考场
            },
          },
          {
            path: 'arranged',
            name: 'examArranged',
            component: () => import('@/views/backExam/arranged.vue'),
            meta: {
              title: '', // 已安排考场
            },
          },
          {
            path: 'unarranged',
            name: 'examUnarranged',
            component: () => import('@/views/backExam/unarranged.vue'),
            meta: {
              title: '', // 未安排考场
            },
          },
        ],
      },
      {
        path: 'score',
        name: 'score',
        component: () => import('@/views/score/index.vue'),
        meta: {
          title: '', // 考级成绩查询
        },
      },
      {
        path: 'performance',
        name: 'performance',
        component: () => import('@/views/performance/index.vue'),
        meta: {
          title: '', // 展演
        },
        children: [
          {
            path: 'org',
            name: 'performanceOrg',
            component: () => import('@/views/performance/org.vue'),
            meta: {
              title: '', // 展演机构
            },
          },
          {
            path: 'manage',
            name: 'performanceManage',
            component: () => import('@/views/performance/manage.vue'),
            meta: {
              title: '', // 展演管理
            },
          },

          {
            path: 'registration',
            name: 'performanceRegistration',
            component: () => import('@/views/performance/registration.vue'),
            meta: {
              title: '', // 展演报名
            },
          },
        ],
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || mainStore.pageTitle;
  next();
});

export default router;
